package member.order.action;

import m.common.action.ActionRequest;
import m.common.action.Param;
import m.common.action.RequestMethod;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import member.base.action.BaseAction;
import member.order.model.RechargeOffset;
import member.order.model.StoreSettle;
import member.order.service.StoreSettleService;

@ActionRequest(name = "action/order/rechargeOffset",description = "充值核销")
public class RechargeOffsetAction extends BaseAction {
    @RequestMethod(description = "列表")
    public JSONMessage getAddSettleList() throws Exception {
        String settleOid=getParamsValue("settleOid");
        if(StringUtil.isSpace(settleOid)) throw new MException("参数错误");
        String inputStoreOid=getParamsValue("inputStoreOid");
        if(StringUtil.isSpace(inputStoreOid)) throw new MException("参数错误");
        String settleStoreOid=getParamsValue("settleStoreOid");
        if(StringUtil.isSpace(settleStoreOid)) throw new MException("参数错误");
        ModelQueryList queryList=toQueryList(RechargeOffset.class,"*","recharge.*","order.*",
                "recharge.inputStore.color","recharge.inputStore.name","order.inputStore.color","order.inputStore.name");
        queryList.addCondition(QueryCondition.eq("status",StoreSettle.StatusSave));
        queryList.addCondition(QueryCondition.or(
            QueryCondition.eq("settle.oid",settleOid),
            QueryCondition.and(
                QueryCondition.isEmpty("settle.oid"),
                QueryCondition.or(
                    QueryCondition.and(
                        QueryCondition.eq("recharge.inputStore.oid",inputStoreOid),
                        QueryCondition.eq("order.inputStore.oid",settleStoreOid)
                    ),
                    QueryCondition.and(
                        QueryCondition.eq("recharge.inputStore.oid",settleStoreOid),
                        QueryCondition.eq("order.inputStore.oid",inputStoreOid)
                    )
                )
            )
        ));
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "列表")
    public JSONMessage getSettleList() throws Exception {
        String settleOid=getParamsValue("settleOid");
        if(StringUtil.isSpace(settleOid)) throw new MException("参数错误");
        ModelQueryList queryList=toQueryList(RechargeOffset.class,"*","recharge.*","order.*",
                "recharge.inputStore.color","recharge.inputStore.name","order.inputStore.color","order.inputStore.name");
        queryList.addCondition(QueryCondition.eq("settle.oid",settleOid));
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "添加结算",transaction = true)
    public JSONMessage addSettle(@Param("oids") String[] oids,@Param("settleOid") String settleOid) throws Exception {
        StoreSettle settle=ModelQueryList.getModel(StoreSettle.class,settleOid,"*");
        if(null==settle) throw new MException("参数错误");
        int n=0;
        for(String oid : oids) {
            RechargeOffset model = ModelQueryList.getModel(RechargeOffset.class, oid, "*");
            if (null == model) continue;
            if (ObjectUtil.notEmptyOid(model.getSettle())) continue;
            if (!StoreSettle.StatusSave.equals(model.getStatus())) continue;
            model.setSettle(settle);
            ModelUpdateUtil.updateModel(model, "settle.oid");
            n++;
        }
        getService(StoreSettleService.class).updateAmount(settle);
        return JSONMessage.success(n>0?"添加成功":"没有可添加的");
    }
    @RequestMethod(description = "取消结算",transaction = true)
    public JSONMessage removeSettle(@Param("oid") String oid) throws Exception {
        RechargeOffset model=ModelQueryList.getModel(RechargeOffset.class,oid,"*","settle.*");
        if(null==model) throw new MException("参数错误");
        if(ObjectUtil.isEmptyOid(model.getSettle())) throw new MException("该条记录未添加");
        StoreSettle settle=model.getSettle();
        model.setSettle(null);
        ModelUpdateUtil.updateModel(model,"settle.oid");
        getService(StoreSettleService.class).updateAmount(settle);
        return JSONMessage.success("取消成功");
    }
}
